/*
 * Created on 2005-3-9
 */
package com.hd123.rumba.runtime;

import javax.naming.NamingException;

import net.sf.hibernate.HibernateException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
import net.sf.hibernate.Transaction;
import net.sf.hibernate.cfg.Configuration;

import org.apache.log4j.Logger;
import org.w3c.dom.Document;

/**
 * @author caili
 */
public class HibernateSession {

  public static SessionFactory sessionFactory;
  protected static final ThreadLocal session = new ThreadLocal();

  // static {
  // try {
  // setEnabled("true");
  // } catch (Throwable th) {
  // throw new ExceptionInInitializerError(th);
  // }
  // }

  public static void setEnabled(String enabled) throws HibernateException {
    if ("true".equals(enabled)) {
      if (sessionFactory == null) {
        sessionFactory = new Configuration().configure(
            HibernateSession.class.getClassLoader().getResource("hibernate.cfg.xml"))
            .buildSessionFactory();
        Logger.getLogger(HibernateSession.class).info("Hibernate session initialized.");
      }
    }
  }

  public static void buildSessionFactory(Document doc) throws HibernateException {
    if (sessionFactory == null) {
      sessionFactory = new Configuration().configure(doc).buildSessionFactory();
      Logger.getLogger(HibernateSession.class).info("--------------------------------");
      Logger.getLogger(HibernateSession.class).info("Hibernate session initialized.");
      Logger.getLogger(HibernateSession.class).info("--------------------------------");
    }
  }

  public static Session getSession() throws HibernateException {

    Session s = (Session) session.get();
    if (s == null) {
      Logger.getLogger(HibernateSession.class).debug(
          "Hibernate session_get." + Thread.currentThread().getId());
      s = sessionFactory.openSession();
      session.set(s);
    }
    return s;
  }

  public static void closeSession() throws HibernateException {
    Session s = (Session) session.get();
    session.set(null);
    if (s != null) {
      s.close();
      Logger.getLogger(HibernateSession.class).debug(
          "Hibernate session_close." + Thread.currentThread().getId());
    }
  }

  public static Transaction beginTransaction() throws HibernateException {
    return getSession().beginTransaction();
  }

  public static void commit(Transaction tx) throws HibernateException {
    tx.commit();
    getSession().clear();
    // Logger.getLogger(HibernateSession.class).info(
    // "Hibernate session_cleared." + Thread.currentThread().getId());
  }

  public static void commit(Transaction tx, boolean clear) throws HibernateException,
      NamingException {
    tx.commit();
    if (clear) {
      getSession().clear();
      // Logger.getLogger(HibernateSession.class).info(
      // "Hibernate session_cleared." + Thread.currentThread().getId());
    }
  }

  public static void rollback(Transaction tx) throws HibernateException {
    tx.rollback();
    getSession().clear();
    // Logger.getLogger(HibernateSession.class).info(
    // "Hibernate session_cleared." + Thread.currentThread().getId());
  }

  public static void rollback(Transaction tx, boolean clear) throws HibernateException {
    tx.rollback();
    if (clear) {
      getSession().clear();
      // Logger.getLogger(HibernateSession.class).info(
      // "Hibernate session_cleared." + Thread.currentThread().getId());
    }
  }
}
